package com.mes.web.controller.jy;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.gson.Gson;
import com.mes.entity.cb.Materiel;
import com.mes.entity.hy.PeopleManage;
import com.mes.entity.hy.Work;
import com.mes.service.jy.AllwServise;
import com.mes.service.jy.TempproductServise;

import com.mes.utils.ResponseUtil;

/**
 * @ClassName: AllwController 
 * @Description:作业信息
 * @Author jy
 * @Date 2018年3月27日 下午4:33:53 
 * @version 1.0
 */
@Controller
@RequestMapping("/allw")
public class AllwController {
	
	@Resource
	private AllwServise allwServise;
	@Resource
	private TempproductServise tempproductServise;

	/**
	 * 去排作业信息页面
	 * @return 作业信息页面
	 */
	public String goAllw () {
		return "jyy/allw";
	}
	
	/**
	 * 添加作业信息 以及 员工管理信息
	 * @param work 作业信息实体
	 * @param response 响应
	 * @throws IOException 输入输出异常
	 */
	@RequestMapping("/createWork")
	public void createWork(Work work,HttpServletResponse response) throws IOException {
		//保存从生产出第一个产品开始保存的
		
		//左边产品的良品数量
		String good = work.getGood();
		
		//右边产品的良品数量
		String goodr = work.getGoodr();
		
		//待入库数量右
		work.setRemartk5(good);
		
		//待入库数量左
		work.setRemartk6(goodr);
		
		//是否入库左
		work.setRemartk7("0");
		
		//是否入库右
		work.setRemartk8("0");
		
		int affact3 = 0;
		//获取当前时间
		Date date = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String format2 = format.format(date);
		String time = dateFormat.format(date);
		
		//排产详情id
		String did = work.getDid();
		
		//remartk 班次查单子中的班组信息
		String remartk = allwServise.selectClazz(did);
		work.setRemartk(remartk);
	   //根据排产详情id查询此信息是否已存在，存在修改，不存在再进行添加
		Work work2 = allwServise.selectByDid(did);
		
		if(work2 != null) {
			//说明存在，进行修改
			Long id = work2.getId();
			work.setId(id);
			allwServise.editAllw(work);
			
			//根据remartk 修改员工信息
			work.setDatetime(format2);
			PeopleManage peopleManage3 = new PeopleManage();
			peopleManage3.setRemartk(work.getDid());
			peopleManage3.setEndgood(good);
			peopleManage3.setEndngood(work.getNgood());
			peopleManage3.setWorknum(work.getSnum());
			peopleManage3.setEndtime(time);
			peopleManage3.setWorkstate("工作中");
			 allwServise.editPeopleManage(peopleManage3);
			 
			//人员管理（第2个）
			if(work.getRemartk2() != null && work.getWoker2() != "") {
				if(work.getWoker2() != null) {
					PeopleManage peopleManage4 = new PeopleManage();
					peopleManage4.setRemartk(work.getRemartk2());
					peopleManage4.setEndgood(goodr);
					peopleManage4.setEndngood(work.getNgoodr());
					peopleManage4.setWorknum(work.getSnumr());
					peopleManage4.setEndtime(time);
					peopleManage4.setWorkstate("工作中");
					affact3 = allwServise.editPeopleManage(peopleManage4);
				}
			}
		}else {
			//添加
			//人员管理用（第1个）
			work.setDatetime(format2);
			PeopleManage peopleManage = new PeopleManage();
			peopleManage.setMachin(work.getMachin());
			peopleManage.setNdate(format2);
			peopleManage.setPeoplenum(work.getWoker1());
			peopleManage.setPname(work.getPname());
			peopleManage.setPnum(work.getPnum());
			peopleManage.setStarttime(time);
			peopleManage.setWorkstate("作业中");
			peopleManage.setRemartk(work.getDid());
			peopleManage.setRemartk1(work.getMachin());
			//人员管理用（第2个）
			if(work.getWoker2() != null && work.getWoker2() != "") {
				PeopleManage peopleManage2 = new PeopleManage();
				peopleManage2.setMachin(work.getMachin());
				peopleManage2.setNdate(format2);
				peopleManage2.setPeoplenum(work.getWoker2());
				peopleManage2.setPname(work.getPnamer());
				peopleManage2.setPnum(work.getPnumr());
				peopleManage2.setStarttime(time);
				peopleManage2.setWorkstate("作业中");
				peopleManage2.setRemartk(work.getRemartk2());
				peopleManage2.setRemartk1(work.getMachin());
				affact3 = allwServise.createPeopleManage(peopleManage2);
			}
			
			int affact = allwServise.createWork(work);
			int affact2 = allwServise.createPeopleManage(peopleManage);
			
			/*修改排产详情状态*/
			allwServise.editDstate(work.getDid());
			
			/*修改计划单详情状态，并带回对应销售计划详情id*/
			String sid = allwServise.editPstate(work.getPid());
			
			/*修改销售计划单状态 状态改成1*/
			allwServise.editSstate(sid);
			
			if(work.getRemartk2() != null) {
				allwServise.editDstate(work.getRemartk2());
				String sid2 = allwServise.editPstate(work.getRemartk1());
				allwServise.editSstate(sid2);
			}
			
			if(affact == 1 && affact2 == 1 && affact3==1 ) {
				response.getWriter().print(1);
			}else {
				response.getWriter().print(0);
			}
		}
	}
	
	/**
	 * 关闭作业信息
	 * @param work 作业信息实体
	 * @param response 响应
	 * @throws IOException 输入输出异常
	 */
	@RequestMapping("/closeWork")
	public void closeWork(Work work,HttpServletResponse response) throws IOException {
		
		Date date = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = dateFormat.format(date);
		
		int affact = 0;
		
		if(work != null) {
			if(work.getPnum() != null && work.getPnum() != "" && work.getSnum() != null && work.getSnum() != "") {
				//左计划量
				Integer pnum = Integer.valueOf(work.getPnum());
				//左实际量
				Integer  snum = Integer.valueOf(work.getSnum());
				 if(pnum > snum) {
					//修改左相关的计划单、排产单为中止状态,并修改计划详情中的待排产数量
					work.setRemartk11((pnum - snum)+"");
					 allwServise.stopWork(work);
				 }else {
					//根据排产单详情id 修改相关模块数据状态 为生产完成。(销售，生产计划，生产排产)
					allwServise.closeWork(work);
				}
			}
			//获取结束时间，更改员工状态为空闲，
			//根据remartk 修改员工信息
			PeopleManage peopleManage3 = new PeopleManage();
			
			peopleManage3.setRemartk(work.getDid());
			peopleManage3.setEndtime(time);
			peopleManage3.setWorkstate("空闲");
			allwServise.editPeopleManage(peopleManage3);
			
		}
		response.getWriter().print(1);
		
	}
	
	/**
	 * 查询产品编号进行BOM计算
	 * @param pnum  产品编号
	 * @param response 响应
	 * @throws Exception 异常
	 */
	@RequestMapping("/selectBom")
	public void selectBom(String pnum,HttpServletResponse response) throws Exception {
		
		//根据产品编号查询这个产品对应的物料集合
		List<Materiel> list= tempproductServise.selectBomByPnum(pnum);
		if(list.size() > 0) {
			Materiel materiel = list.get(0);
			Gson gson = new Gson();
			String json = gson.toJson(materiel);
			ResponseUtil.write(response, json);
		}
	}
}
